package lanqiao.dontaiguihua.dfs;
import java.util.*;

//移动字母
public class Lan280 {
    static Scanner sc=new Scanner(System.in);
    static int[] a={3,-3,1,-1};
    static int n;
    static String fin="ABCDE*";
    public static void main(String[] args) {

        n=sc.nextInt();
        for(int i=0;i<n;i++){
            String s=sc.next();
            bfs(s);
        }
        sc.close();
    }
   /*   3
    ABCDE*
    AB*DEC
    CAED*B*/
    public static void bfs(String s){
        Queue<String> queue=new LinkedList<>();
        Set<String> set=new HashSet<>();
        queue.add(s);
        set.add(s);
        while(!queue.isEmpty()){
            for(int i=queue.size();i>0;i--){
                String s1=queue.poll();
                if(s1.equals(fin)){
                    System.out.println(1);
                    return ;
                }
                int index=s1.indexOf("*");
                for(int j=0;j<a.length;j++) {
                    int indexof=index+a[j];
                    if(indexof<0||indexof>s.length()-1) continue;
                    char[] k = s1.toCharArray();
                    char o=k[indexof];
                    k[indexof]=k[index];
                    k[index]=o;
                    String s2=String.valueOf(k);
                    if(set.contains(s2)){
                        continue;
                    }
                    queue.add(s2);
                    set.add(s2);
                }
            }

        }

        System.out.println(0);
    }
}
